package com.slwish.xty.mvp.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.common.utils.LogUtils;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.entity.UserCommentListModel;
import com.slwish.xty.mvp.contract.UserCommentContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * 模块说明：
 *
 * @author 陈浩
 * @date 2017/11/30
 */

public class UserCommentPresenter extends BasePresenter<UserCommentContract.View> implements UserCommentContract.Presenter {
    public UserCommentPresenter(UserCommentContract.View view) {
        super(view);
    }

    @Override
    public void queryCommentListByMerchantId(String merId, int page) {
        //处理账号及密码

        EasyHttp.post(UrlConfig.QUERY_COMMENT_BY_MERCHANT_URL)
                .params("merId", merId)
                .params("page", page + "")
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String json) {
                        LogUtils.i("xx", "json:" + json);
                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            String data = jsonObject.getString("data");
                            //return
                            if (!TextUtils.equals(resultCode, "0")) {
                                setError(resultMessage);
//                                return;
                            }
                            List<UserCommentListModel> model = JSON.parseArray(data, UserCommentListModel.class);
                            if (getView() != null) {
                                getView().querySuccess(model);
//                                setError(R.string.login_success_text);
                            }
                        } catch (Exception e) {
//                            setError(R.string.loadig_failed_text);
                            if (getView()!=null){
                                getView().queryFailed();
                            }
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
//                        setError(R.string.loadig_failed_text);
                        if (getView()!=null){
                            getView().queryFailed();
                        }
                    }
                });
    }
}
